package com.bgs.service.impl;

import com.bgs.dao.EmployeeInfoMapper;
import com.bgs.dao.EmployeeRegisterMapper;
import com.bgs.pojo.EmployeeInfo;
import com.bgs.pojo.EmployeeRegister;
import com.bgs.service.ERegisterService;
import com.bgs.utils.PasswordManagerUtil;
import com.bgs.utils.SerialNumberUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class ERegisterServiceImpl implements ERegisterService {

    Logger log = LogManager.getLogger();

    @Autowired
    //注册用户账号密码表
    private EmployeeRegisterMapper employeeRegisterMapper;

    @Autowired
    //用户具体的表
    private EmployeeInfoMapper employeeInfoMapper;


    @Override
    public EmployeeRegister login(EmployeeRegister employeeRegister) {

       EmployeeRegister e = employeeRegisterMapper.findLogin(employeeRegister);
    return e;
    }

    @Override
    public Map<String, Object> saveRegister(String username, String email, String password) {

        //生成一个随机的userId // 员工ID
        String userId = SerialNumberUtil.getUUID();

        // 密码,md5加密
        String passMD5 = PasswordManagerUtil.getMd5DigestAsHex(password);


        // 注册表
        EmployeeRegister employeeRegister = new EmployeeRegister();
        employeeRegister.setEmail(email);
        employeeRegister.setPassword(passMD5);
        employeeRegister.setUserId(userId);
        int iReg = employeeRegisterMapper.insertSelective(employeeRegister);
        log.info("insertSelective-iReg::: " + iReg);

        // 信息表
        EmployeeInfo employeeInfo = new EmployeeInfo();
        employeeInfo.setEmployeeName(username);
        employeeInfo.setEmail(email);
        employeeInfo.setUserId(userId);
        log.info("insertSelective-employeeInfo::: " + employeeInfo);
        int iInfo = employeeInfoMapper.insertSelective(employeeInfo);
        log.info("insertSelective-iReg::: " + iInfo);

        Map<String, Object> m = new HashMap<>();

        m.put("code","0000");
        m.put("msg","交易成功");

        return m;
    }


}
